package com.bym.mall.controller;

import com.bym.mall.form.CartAddForm;
import com.bym.mall.form.CartUpdateForm;
import com.bym.mall.service.ICartService;
import com.bym.mall.vo.CartVo;
import com.bym.mall.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 购物车模块
 *
 * @author My
 * @date 2022/03/26
 */
@Api("购物车模块")
@Slf4j
@RestController()
@RequestMapping("/mall/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @ApiOperation("展示购物车信息（最新）")
    @GetMapping("/list")
    public ResponseVo<CartVo> list() {
        return cartService.newList();
    }

    @ApiOperation("新增购物车中的商品")
    @PostMapping("/add")
    public ResponseVo<CartVo> add(@RequestBody @Valid CartAddForm cartAddForm) {
        return cartService.add(cartAddForm);
    }

    @ApiOperation("更新购物车中的商品")
    @PutMapping("/update")
    public ResponseVo<CartVo> update(@RequestBody @Valid CartUpdateForm cartUpdateForm) {
        return cartService.update(cartUpdateForm);
    }

    @ApiOperation("删除购物车中的商品")
    @DeleteMapping("/delete")
    public ResponseVo<CartVo> delete(@RequestParam Integer productId) {
        return cartService.delete(productId);
    }

    @ApiOperation("购物车商品全选中")
    @PutMapping("/selectAll")
    public ResponseVo<CartVo> selectAll() {
        return cartService.selectAll();
    }

    @ApiOperation("购物车商品全不选中")
    @PutMapping("/unSelectAll")
    public ResponseVo<CartVo> unSelectAll() {
        return cartService.unSelectAll();
    }

    @ApiOperation("购物车中商品的总和")
    @GetMapping("/sum")
    public ResponseVo<Integer> sum() {
        return cartService.sum();
    }

    @ApiOperation("持久化购物车")
    @GetMapping("/persistCart")
    public ResponseVo persistCart() {
        return cartService.persistCart();
    }

    @ApiOperation("缓存购物车")
    @GetMapping("/cacheCart")
    public ResponseVo cacheCart() {
        return cartService.cacheCart();
    }
}